package com.myself.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.util.HashMap;

/**
 * @author W.S.
 * @version v4.0.0
 * @description
 * @date Created in 2018/9/4 13:52
 */
public class LearningNIO  {

    public String filePath = "/Users/wensai/Documents/222.txt";

    public String outFilePath = "/Users/wensai/Documents/test.txt";

    public int capacity = 100;

    private final byte message[] = { 83, 111, 109, 101, 32,
            98, 121, 116, 101, 115, 46 };


    public void test1() throws IOException{
        FileInputStream inputStream = new FileInputStream(filePath);
        FileChannel fileChannel = inputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(100);
        int length = fileChannel.read(byteBuffer);
        byteBuffer.flip();
        //获取文件输出流
        FileOutputStream outputStream = new FileOutputStream(outFilePath);
        //获取输出的文件通道
        FileChannel outChannel = outputStream.getChannel();
        outChannel.write(byteBuffer);
        outputStream.close();
        inputStream.close();
    }

    public void test2() throws IOException{

        //获取文件输出流
        FileOutputStream outputStream = new FileOutputStream(outFilePath);
        //获取输出的文件通道
        FileChannel outChannel = outputStream.getChannel();
        //初始化缓存区 100大小
        ByteBuffer outBuffer = ByteBuffer.allocate(capacity);
        for (int i=0; i<message.length; ++i) {
            outBuffer.put( message[i] );
        }
        outBuffer.flip();

    }

    public void test3() throws IOException{
        FileInputStream inputStream = new FileInputStream(filePath);
        FileChannel channel = inputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(capacity);
        channel.read(byteBuffer);
        byteBuffer.flip();
        for(int i = 0 ; i<2;i++){
            System.out.print((char)byteBuffer.get());

        }
        System.out.println();
        byteBuffer.compact();
        byteBuffer.flip();
        System.out.println("   compact:  "+(char)byteBuffer.get());
        System.out.println("------------");
    }

    /**
     * 子集缓存区
     * @throws IOException
     */
    public void test4() throws IOException{
        IntBuffer intBuffer = IntBuffer.allocate(10);
        for(int i = 0 ; i<intBuffer.capacity();i++){
            intBuffer.put(i);
        }
        intBuffer.limit(7);
        outPut(intBuffer);
        IntBuffer slice = intBuffer.slice();
        outPut(slice);

    }

    /**
     * NIO中非阻塞I/O编写服务器处理程序，大体上可以分为下面三个步骤：
     * 1. 向Selector对象注册感兴趣的事件
     * 2. 从Selector中获取感兴趣的事件
     * 3. 根据不同的事件进行相应的处理
     * @throws IOException
     */
    public void test5() throws IOException{
        //打开一个ServerSocketChannel 通道
        ServerSocketChannel socketChannel = ServerSocketChannel.open();
        //关闭serbersocket通道
        //socketChannel.close();

    }


    private void outPut(Buffer buffer){
        System.out.print("capacity: " + buffer.capacity() + ", ");
        System.out.print("position: " + buffer.position() + ", ");
        System.out.println("limit: " + buffer.limit());
        System.out.println();

    }

    public static void main(String[] args) {
        LearningNIO nio = new LearningNIO();
        HashMap a = new HashMap();
        a.put("12","13");
        try {
            //nio.test1();
            //nio.test2();
            //nio.test3();
            nio.test4();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
